import 'package:boss/textStyle/text_style.dart';
import 'package:boss/ui/page/analysis/provider/analysis_provider.dart';
import 'package:boss/ui/page/analysis/widget/commodity/commodity_header.dart';
import 'package:boss/ui/page/analysis/widget/commodity/commodity_row.dart';
import 'package:boss/ui/page/analysis/widget/modal/analysis_staff_sell_modal.dart';
import 'package:boss/ui/page/analysis/widget/time_row.dart';
import 'package:boss/ui/widget/image/base_image.dart';
import 'package:boss/ui/widget/loader/loader_pull_refresh.dart';
import 'package:hi_common/hi_common.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:boss/ui/widget/popupMenu/popupMenu.dart' as popupMenu;

class CommodityBrowser extends StatefulWidget {
  @override
  _CommodityBrowserState createState() => _CommodityBrowserState();
}

class _CommodityBrowserState extends State<CommodityBrowser> with AutomaticKeepAliveClientMixin {
  @override
  Widget build(BuildContext context) {
    super.build(context);

    return Consumer<AnalysisProvider>(
      builder: (context, provider, child) {
        return Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: <Widget>[
            Container(
              margin: EdgeInsets.only(top: 30.w, left: 50.w),
              child: TimeRow(provider.timerRangeText),
            ),
            Container(
              padding: EdgeInsets.only(left: 50.w, bottom: 20.w, right: 30.w),
              child: Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: <Widget>[
                  Text('菜品销量排行', style: TextStyleConstant().normal_1_34),
                  Consumer<AnalysisProvider>(
                    builder: (context, provider, child) {
                      return Row(
                        children: <Widget>[
                          _PopMenuButton(
                            provider.commoditySelectedType == 0 ? '按菜品' : '按分类',
                            menus: [_PopMenuData('按分类', 1), _PopMenuData('按菜品', 0)],
                            value: provider.commoditySelectedType,
                            onSelected: provider.setCommodityType,
                          ),
                          _PopMenuButton(
                            provider.takeOutSelectedType == 0 ? '不含外卖' : '含外卖',
                            menus: [_PopMenuData('含外卖', 1), _PopMenuData('不含外卖', 0)],
                            value: provider.takeOutSelectedType,
                            onSelected: provider.setTakeOutType,
                          ),
                          _PopMenuButton(
                            provider.countSelectedType == 0 ? '按金额' : '按销量',
                            menus: [_PopMenuData('按金额', 0), _PopMenuData('按销量', 1)],
                            value: provider.countSelectedType,
                            onSelected: provider.setCountType,
                          ),
                        ],
                      );
                    },
                  ),
                ],
              ),
            ),
            Expanded(
              child: LoaderPullRefresh(
                refreshTask: provider.querySortCommodityCells,
                builder: (context) {
                  return ListView.builder(
                    padding: EdgeInsets.symmetric(horizontal: 30.w),
                    itemBuilder: (context, index) {
                      if (index == 0) {
                        return CommodityHeader();
                      }

                      var current = provider.commoditys[index - 1];

                      return CommodityRow(
                        current,
                        onTap: () {
                          showAnalysisDetailModal(
                            context,
                            startTime: provider.httpStartTimeText,
                            endTime: provider.httpEndTimeText,
                            commodityOrMenu: provider.commoditySelectedType,
                            takeOutType: current.type,
                            sortType: provider.countSelectedType,
                            queryID: current.id,
                            name: current.name,
                          );
                        },
                      );
                    },
                    itemCount: provider.commoditys.length + 1,
                  );
                },
              ),
            )
          ],
        );
      },
    );
  }

  @override
  bool get wantKeepAlive => true;
}

class _PopMenuButton<T> extends StatelessWidget {
  final String title;
  final List<_PopMenuData<T>> menus;
  final T value;
  final PopupMenuItemSelected<T> onSelected;

  const _PopMenuButton(
    this.title, {
    this.menus,
    this.value,
    this.onSelected,
  });

  @override
  Widget build(BuildContext context) {
    return popupMenu.PopupMenuButton(
      child: Container(
        margin: EdgeInsets.only(
          left: 20.w,
        ),
        child: Row(
          children: <Widget>[
            Text(
              title,
              style: TextStyleConstant().normal_2_26,
            ),
            Container(
              margin: EdgeInsets.only(
                left: 8.w,
              ),
              child: BaseImage(
                'assets/image/arrow_down_icon.png',
                width: 22.w,
                height: 10.w,
              ),
            ),
          ],
        ),
      ),
      items: menus.map((item) => item.title).toList(),
      index: menus.indexWhere((item) => item.value == value),
      onItemTap: (index) {
        if (onSelected != null) {
          onSelected(menus[index].value);
        }
      },
    );
  }
}

class _PopMenuData<T> {
  String title;
  T value;

  _PopMenuData(this.title, this.value);
}
